//
//  UIViewController+YDAlert.m
//  Merchants
//
//  Created by Yigol on 2018/5/21.
//  Copyright © 2018年 yida. All rights reserved.
//

#import "UIViewController+YDAlert.h"
#import "CommonAlertController.h"
#import "CommonSelectiveAlertViewController.h"
#import <objc/runtime.h>


static const void *controllerKey = &controllerKey;

@interface UIViewController ()

@property (nonatomic, strong) UIViewController *controller;

@end

@implementation UIViewController (YDAlert)

#pragma mark - ************ Simple
- (void)yd_showNoticeAlertWithMessage:(nullable NSString *)message handler:(void (^ __nullable)(void))hander {
    [self yd_showAlertWithTitle:@"通知" message:message type:YDAlertTypeNotice firstHandlerName:@"确定" secondHandlerName:nil firstHandler:hander secondHandler:nil];
}

- (void)yd_showWarningAlertWithMessage:(nullable NSString *)message handler:(void (^ __nullable)(void))hander {
    [self yd_showAlertWithTitle:@"提示" message:message type:YDAlertTypeWarning firstHandlerName:@"确定" secondHandlerName:nil firstHandler:hander secondHandler:nil];
}

- (void)yd_showSuccessAlertWithMessage:(nullable NSString *)message handler:(void (^ __nullable)(void))hander {
    [self yd_showAlertWithTitle:@"成功" message:message type:YDAlertTypeSuccess firstHandlerName:@"确定" secondHandlerName:nil firstHandler:hander secondHandler:nil];
}

- (void)yd_showSelectiveAlertWithMessage:(nullable NSString *)message okHandler:(void (^ __nullable)(void))handler {
    [self yd_showAlertWithTitle:nil message:message type:YDAlertTypeSelective firstHandlerName:@"确定" secondHandlerName:@"取消" firstHandler:handler secondHandler:nil];
}

#pragma mark - ************ General
- (void)yd_showNoticeAlertWithTitle:(nullable NSString *)title message:(nullable NSString *)message handler:(void (^ __nullable)(void))hander {
    [self yd_showAlertWithTitle:title message:message type:YDAlertTypeNotice firstHandlerName:@"确定" secondHandlerName:nil firstHandler:hander secondHandler:nil];
}

- (void)yd_showWarningAlertWithTitle:(nullable NSString *)title message:(nullable NSString *)message handler:(void (^ __nullable)(void))hander {
    [self yd_showAlertWithTitle:title message:message type:YDAlertTypeWarning firstHandlerName:@"确定" secondHandlerName:nil firstHandler:hander secondHandler:nil];
}

- (void)yd_showSuccessAlertWithTitle:(nullable NSString *)title message:(nullable NSString *)message handler:(void (^ __nullable)(void))hander {
    [self yd_showAlertWithTitle:title message:message type:YDAlertTypeSuccess firstHandlerName:@"确定" secondHandlerName:nil firstHandler:hander secondHandler:nil];
}

- (void)yd_showSelectiveAlertWithMessage:(nullable NSString *)message firstHandler:(void (^)(void))firstHandler secondHandler:(void (^ __nullable)(void))secondHandler {
    [self yd_showAlertWithTitle:nil message:message type:YDAlertTypeSelective firstHandlerName:@"确定" secondHandlerName:@"取消" firstHandler:firstHandler secondHandler:secondHandler];
}

#pragma mark - ************ 通用

- (void)yd_showAlertWithTitle:(nullable NSString *)title
                      message:(nullable NSString *)message
                         type:(YDAlertType)type
             firstHandlerName:(nullable NSString *)firstName
            secondHandlerName:(nullable NSString *)secondName
                 firstHandler:(void (^ __nullable)(void))firstHandler
                secondHandler:(void (^ __nullable)(void))secondHandler {
    
    switch (type) {
        case YDAlertTypeNotice:
        {
            CommonAlertController *vc = [CommonAlertController loadController];
            vc.iconImageView.image = [UIImage imageNamed:@"icon_notice"];
            vc.titles = title;
            vc.messages = message;
            vc.firstHandlerName = firstName;
            vc.firstHandler = firstHandler;
            self.controller = vc;
        }
            break;
        case YDAlertTypeWarning:
        {
            CommonAlertController *vc = [CommonAlertController loadController];
            vc.iconImageView.image = [UIImage imageNamed:@"icom_warning"];
            vc.titles = title;
            vc.messages = message;
            vc.firstHandlerName = firstName;
            vc.firstHandler = firstHandler;
            self.controller = vc;
        }
            break;
        case YDAlertTypeSuccess:
        {
            CommonAlertController *vc = [CommonAlertController loadController];
            vc.iconImageView.image = [UIImage imageNamed:@"icon_complete"];
            vc.titles = title;
            vc.messages = message;
            vc.firstHandlerName = firstName;
            vc.firstHandler = firstHandler;
            self.controller = vc;
        }
            break;
        case YDAlertTypeSelective:
        {
            CommonSelectiveAlertViewController *vc = [CommonSelectiveAlertViewController loadController];
            vc.messages = message;
            vc.firstHandlerName = firstName;
            vc.secondHandlerName = secondName;
            vc.firstHandler = firstHandler;
            vc.secondHandler = secondHandler;
            self.controller = vc;
        }
            break;
            
    }
    
    [self presentViewController:self.controller animated:true completion:nil];
}

#pragma mark - ************ 消失
- (void)yd_dismiss {
    [self.controller dismissViewControllerAnimated:true completion:nil];
}

- (void)yd_dissmissAfter:(NSTimeInterval)time {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self yd_dismiss];
    });
}

#pragma mark - ************ Setter && Getter
- (void)setController:(UIViewController *)controller {
    objc_setAssociatedObject(self, controllerKey, controller, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIViewController *)controller {
    return objc_getAssociatedObject(self, controllerKey);
}

@end
